

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=&#34;auto&#34;>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/zoro.jpg">
  <link rel="icon" href="/img/zoro.jpg">
  <meta name="viewport"
        content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#efb9b9">
  <meta name="description" content="">
  <meta name="author" content="AgoniLay">
  <meta name="keywords" content="">
  
  <title>ES6笔记 - AgoniLay的博客</title>

  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/css/bootstrap.min.css" />


  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/github-markdown-css@4.0.0/github-markdown.min.css" />
  <link  rel="stylesheet" href="/lib/hint/hint.min.css" />

  
    
    
      
      <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/highlight.js@10.7.2/styles/arta.min.css" />
    
  

  
    <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.5.7/dist/jquery.fancybox.min.css" />
  



<!-- 主题依赖的图标库，不要自行修改 -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_ba1fz6golrf.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_kmeydafke9r.css">


<link  rel="stylesheet" href="/css/main.css" />

<!-- 自定义样式保持在最底部 -->

  
<link rel="stylesheet" href="/css/scroller.css">
<link rel="stylesheet" href="/css/bynode.css">
<link rel="stylesheet" href="/css/mouse.css">



  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    var CONFIG = {"hostname":"agonilay.github.io","root":"/","version":"1.8.11","typing":{"enable":true,"typeSpeed":150,"cursorChar":"❤","loop":false},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"right","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"copy_btn":true,"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/loading2.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":false,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null}},"search_path":"/local-search.xml"};
  </script>
  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
<meta name="generator" content="Hexo 5.4.0"></head>


<body>
  <header style="height: 70vh;">
    <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand"
       href="/">&nbsp;<strong>AgoniLay</strong>&nbsp;</a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item dropdown">
              <a class="nav-link dropdown-toggle" target="_self" role="button"
                 data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                <i class="iconfont icon-bookmark"></i>
                索引
              </a>
              <div class="dropdown-menu" aria-labelledby="navbarDropdown">
                
                  
                  
                  
                  <a class="dropdown-item" href="/archives/">
                    <i class="iconfont icon-archive-fill"></i>
                    归档
                  </a>
                
                  
                  
                  
                  <a class="dropdown-item" href="/categories/">
                    <i class="iconfont icon-category-fill"></i>
                    分类
                  </a>
                
                  
                  
                  
                  <a class="dropdown-item" href="/tags/">
                    <i class="iconfont icon-tags-fill"></i>
                    标签
                  </a>
                
              </div>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/links/">
                <i class="iconfont icon-link-fill"></i>
                友链
              </a>
            </li>
          
        
          
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" data-toggle="modal" data-target="#modalSearch">&nbsp;<i
                class="iconfont icon-search"></i>&nbsp;</a>
          </li>
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

    <div class="banner" id="banner" parallax=true
         style="background: url('/img/zoro2.jpg') no-repeat center center;
           background-size: cover;">
      <div class="full-bg-img">
        <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
          <div class="page-header text-center fade-in-up">
            <span class="h3" id="subtitle" title="ES6笔记">
              
            </span>

            
              <div class="mt-3">
  
    <span class="post-meta mr-2">
      <i class="iconfont icon-author" aria-hidden="true"></i>
      AgoniLay
    </span>
  
  
    <span class="post-meta">
      <i class="iconfont icon-date-fill" aria-hidden="true"></i>
      <time datetime="2021-07-30 14:59" pubdate>
        2021年7月30日 下午
      </time>
    </span>
  
</div>

<div class="mt-1">
  

  
    
    <span class="post-meta mr-2">
      <i class="iconfont icon-clock-fill"></i>
      
      
      145
       分钟
    </span>
  

  
  
    
      <!-- 不蒜子统计文章PV -->
      <span id="busuanzi_container_page_pv" style="display: none">
        <i class="iconfont icon-eye" aria-hidden="true"></i>
        <span id="busuanzi_value_page_pv"></span> 次
      </span>
    
  
</div>

            
			
			
          </div>

          
        </div>
      </div>
    </div>
  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="d-none d-lg-block col-lg-2"></div>
    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div class="py-5" id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">ES6笔记</h1>
            
            <div class="markdown-body">
              <h2 id="第一章-ECMASript-相关介绍"><a href="#第一章-ECMASript-相关介绍" class="headerlink" title="第一章 ECMASript 相关介绍"></a>第一章 <strong>ECMASript</strong> 相关介绍</h2><h3 id="1-1-什么是-ECMA"><a href="#1-1-什么是-ECMA" class="headerlink" title="1.1 什么是 ECMA"></a>1.1 什么是 ECMA</h3><p>ECMA（European Computer Manufacturers Association）中文名称为欧洲计算机制造商协会，这个组织的目标是评估、开发和认可电信和计算机标准。1994 年后该组织改名为 Ecma 国际。</p>
<h3 id="1-2-什么是-ECMAScript"><a href="#1-2-什么是-ECMAScript" class="headerlink" title="1.2 什么是 ECMAScript"></a>1.2 什么是 ECMAScript</h3><p>ECMAScript 是由 Ecma 国际通过 ECMA-262 标准化的脚本程序设计语言。</p>
<h3 id="1-3-为什么要学习-ES6"><a href="#1-3-为什么要学习-ES6" class="headerlink" title="1.3 为什么要学习 ES6"></a>1.3 为什么要学习 ES6</h3><ul>
<li>ES6 的版本变动内容最多，具有里程碑意义</li>
<li>ES6 加入许多新的语法特性，编程实现更简单、高效</li>
<li>ES6 是前端发展趋势，就业必备技能</li>
</ul>
<h3 id="1-4-ES6-兼容性"><a href="#1-4-ES6-兼容性" class="headerlink" title="1.4 ES6 兼容性"></a>1.4 ES6 兼容性</h3><p><a target="_blank" rel="noopener" href="http://kangax.github.io/compat-table/es6/">http://kangax.github.io/compat-table/es6/</a> 可查看兼容性</p>
<h2 id="第二章-ECMAScript-6-新特性"><a href="#第二章-ECMAScript-6-新特性" class="headerlink" title="第二章 ECMAScript 6 新特性"></a>第二章 ECMAScript 6 新特性</h2><h3 id="2-1-let-关键字"><a href="#2-1-let-关键字" class="headerlink" title="2.1 let 关键字"></a>2.1 let 关键字</h3><h5 id="特性"><a href="#特性" class="headerlink" title="特性"></a>特性</h5><ul>
<li>变量不能重复声明</li>
<li>块儿级作用域</li>
<li>不存在变量提升(不允许在变量声明之前使用变量)</li>
<li>不影响作用域链</li>
</ul>
<h5 id="应用场景"><a href="#应用场景" class="headerlink" title="应用场景"></a>应用场景</h5><p>​    以后声明变量使用 let 就对了</p>
<h3 id="2-2-const-关键字"><a href="#2-2-const-关键字" class="headerlink" title="2.2 const 关键字"></a>2.2 const 关键字</h3><h5 id="特性-1"><a href="#特性-1" class="headerlink" title="特性"></a>特性</h5><ul>
<li>声明必须赋初始值</li>
<li>标识符一般为大写</li>
<li>不允许重复声明</li>
<li>值不允许修改</li>
<li>块儿级作用域</li>
</ul>
<h5 id="注意"><a href="#注意" class="headerlink" title="注意"></a>注意</h5><p>​    对象属性修改和数组元素变化不会出发 const 错误</p>
<h5 id="应用场景-1"><a href="#应用场景-1" class="headerlink" title="应用场景"></a>应用场景</h5><p>​    声明对象类型使用 const，非对象类型声明选择 let</p>
<h3 id="2-3-变量的解构赋值"><a href="#2-3-变量的解构赋值" class="headerlink" title="2.3 变量的解构赋值"></a>2.3 变量的解构赋值</h3><p>ES6 允许按照一定模式，从数组和对象中提取值，对变量进行赋值，这被称为解构赋值。</p>
<figure class="highlight js"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs js"><span class="hljs-comment">//数组的解构赋值</span><br><span class="hljs-keyword">const</span> arr = [<span class="hljs-string">&#x27;张学友&#x27;</span>, <span class="hljs-string">&#x27;刘德华&#x27;</span>, <span class="hljs-string">&#x27;黎明&#x27;</span>, <span class="hljs-string">&#x27;郭富城&#x27;</span>];<br><span class="hljs-keyword">let</span> [zhang, liu, li, guo] = arr;<br><span class="hljs-built_in">console</span>.log(zhang); <span class="hljs-comment">// 张学友</span><br><br><span class="hljs-comment">//对象的解构赋值</span><br><span class="hljs-keyword">const</span> lin = &#123;<br>    <span class="hljs-attr">name</span>: <span class="hljs-string">&#x27;林志颖&#x27;</span>,<br>    <span class="hljs-attr">tags</span>: [<span class="hljs-string">&#x27;车手&#x27;</span>, <span class="hljs-string">&#x27;歌手&#x27;</span>, <span class="hljs-string">&#x27;小旋风&#x27;</span>, <span class="hljs-string">&#x27;演员&#x27;</span>],<br>    <span class="hljs-attr">changge</span>: <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>&#123;<br>        <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&#x27;我可以唱歌&#x27;</span>);<br>    &#125;<br>&#125;;<br><span class="hljs-keyword">let</span> &#123;name, tags&#125; = lin;<br><span class="hljs-built_in">console</span>.log(name); <span class="hljs-comment">// 林志颖</span><br><span class="hljs-keyword">let</span> &#123;cahngge&#125; = lin;<br>changge(); <span class="hljs-comment">// 我可以唱歌</span><br></code></pre></div></td></tr></table></figure>

<h3 id="2-4-模板字符串"><a href="#2-4-模板字符串" class="headerlink" title="2.4 模板字符串"></a>2.4 模板字符串</h3><p>模板字符串（template string）是增强版的字符串，用一对反引号（`）标识</p>
<h5 id="特性-2"><a href="#特性-2" class="headerlink" title="特性"></a>特性</h5><ul>
<li>字符串中可以出现换行符</li>
<li>可以使用 ${xxx} 的输出变量</li>
</ul>
<h3 id="2-5-简化对象写法"><a href="#2-5-简化对象写法" class="headerlink" title="2.5 简化对象写法"></a>2.5 简化对象写法</h3><p>ES6 允许在大括号里面，直接写入变量和函数，作为对象的属性和方法。这样的书写更加简洁。</p>
<figure class="highlight js"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs js"><span class="hljs-keyword">let</span> name = <span class="hljs-string">&#x27;尚硅谷&#x27;</span>;<br><span class="hljs-keyword">let</span> slogon = <span class="hljs-string">&#x27;永远追求行业更高标准&#x27;</span>;<br><span class="hljs-keyword">let</span> improve = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>&#123;<br>    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&#x27;可以提高你的技能&#x27;</span>);<br>&#125;<br><span class="hljs-comment">//属性和方法简写</span><br><span class="hljs-keyword">let</span> atguigu = &#123;<br>    name,<br>    slogon,<br>    improve,<br>    <span class="hljs-function"><span class="hljs-title">change</span>(<span class="hljs-params"></span>)</span> &#123;<br>        <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&#x27;可以改变你&#x27;</span>)<br>    &#125;<br>&#125;;<br></code></pre></div></td></tr></table></figure>

<h3 id="2-6-箭头函数"><a href="#2-6-箭头函数" class="headerlink" title="2.6 箭头函数"></a>2.6 箭头函数</h3><p>ES6 允许使用「箭头」  <code>=&gt;</code>  定义函数。</p>
<p>ES6 允许给函数参数赋初始值</p>
<ul>
<li>具有默认值的参数，一般靠后</li>
<li>可以与解构赋值结合</li>
</ul>
<h5 id="注意点"><a href="#注意点" class="headerlink" title="注意点"></a>注意点</h5><ul>
<li>如果形参只有一个，则小括号可以省略</li>
<li>函数体如果只有一条语句，则花括号可以省略，此时 return 必须省略，而且函数的返回值为该条语句的执行结果</li>
<li>箭头函数 this 是静态的，始终指向声明时所在作用域下 this 的值</li>
<li>箭头函数不能作为构造函数实例化</li>
<li>不能使用 arguments</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs js"><span class="hljs-comment">// 通用写法</span><br><span class="hljs-keyword">let</span> fn = <span class="hljs-function">(<span class="hljs-params">arg1, arg2, arg3</span>) =&gt;</span> &#123;<br>    <span class="hljs-keyword">return</span> arg1 + arg2 + arg3;<br>&#125;<br><span class="hljs-comment">// 省略花括号的情况</span><br><span class="hljs-keyword">let</span> fn3 = <span class="hljs-function"><span class="hljs-params">score</span> =&gt;</span> score * <span class="hljs-number">20</span>;<br></code></pre></div></td></tr></table></figure>

<h5 id="应用场景-2"><a href="#应用场景-2" class="headerlink" title="应用场景"></a>应用场景</h5><p>适合与 this 无关的回调  定时器、数组的方法回调</p>
<p>不适合与 this 有关的回调  事件回调、对象的方法</p>
<h3 id="2-7-rest-参数"><a href="#2-7-rest-参数" class="headerlink" title="2.7 rest 参数"></a>2.7 rest 参数</h3><p>ES6 引入 rest 参数，用于获取函数的实参，用来代替 arguments</p>
<ul>
<li><code>...args</code></li>
<li>rest参数必须要放到最后</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs js"><span class="hljs-comment">/**</span><br><span class="hljs-comment">* 作用与 arguments 类似</span><br><span class="hljs-comment">*/</span><br><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">add</span>(<span class="hljs-params">...args</span>)</span>&#123;<br>    <span class="hljs-built_in">console</span>.log(args); <span class="hljs-comment">// [1,2,3,4,5]</span><br>&#125;<br>add(<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>,<span class="hljs-number">4</span>,<span class="hljs-number">5</span>);<br><span class="hljs-comment">/**</span><br><span class="hljs-comment">* rest 参数必须是最后一个形参</span><br><span class="hljs-comment">*/</span><br><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">minus</span>(<span class="hljs-params">a,b,...args</span>)</span>&#123;<br>    <span class="hljs-built_in">console</span>.log(a);		<span class="hljs-comment">// 100</span><br>    <span class="hljs-built_in">console</span>.log(b);		<span class="hljs-comment">// 1</span><br>    <span class="hljs-built_in">console</span>.log(args);	<span class="hljs-comment">// [2,3,4,5,19]</span><br>&#125;<br>minus(<span class="hljs-number">100</span>,<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>,<span class="hljs-number">4</span>,<span class="hljs-number">5</span>,<span class="hljs-number">19</span>);<br></code></pre></div></td></tr></table></figure>

<h5 id="应用场景-3"><a href="#应用场景-3" class="headerlink" title="应用场景"></a>应用场景</h5><p>rest 参数非常适合不定个数参数函数的场景</p>
<h3 id="2-8-spread-运算符"><a href="#2-8-spread-运算符" class="headerlink" title="2.8 spread 运算符"></a>2.8 spread 运算符</h3><p>扩展运算符（spread）也是三个点<code>...</code>。它好比 rest 参数的逆运算，将一个数组转为用逗号分隔的参数序列，对数组进行解包。</p>
<h5 id="应用"><a href="#应用" class="headerlink" title="应用"></a>应用</h5><ol>
<li><p>数组的合并</p>
<figure class="highlight js"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs js"><span class="hljs-keyword">const</span> arr1 = [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>];<br><span class="hljs-keyword">const</span> arr2 = [<span class="hljs-number">3</span>, <span class="hljs-number">4</span>];<br><span class="hljs-comment">// const hebing = arr1.concat(arr2); 原先api函数</span><br><span class="hljs-keyword">const</span> hebing = [...arr1, ...arr2]<br></code></pre></div></td></tr></table></figure></li>
<li><p>数组的克隆</p>
<figure class="highlight js"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs js"><span class="hljs-keyword">const</span> arr1 = [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>];<br><span class="hljs-comment">// 注意是 浅克隆</span><br><span class="hljs-keyword">const</span> hebing = [...arr1,]<br></code></pre></div></td></tr></table></figure></li>
<li><p>将伪数组转为真正的数组</p>
<figure class="highlight js"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs js"><span class="hljs-keyword">const</span> divs = <span class="hljs-built_in">document</span>.querySelectAll(<span class="hljs-string">&#x27;div&#x27;</span>); <span class="hljs-comment">// 伪数组</span><br><span class="hljs-keyword">const</span> divArr = [...divs]<br></code></pre></div></td></tr></table></figure></li>
</ol>
<h3 id="2-9-Symbol"><a href="#2-9-Symbol" class="headerlink" title="2.9 Symbol"></a>2.9 Symbol</h3><p>ES6 引入了一种新的原始数据类型 Symbol，表示独一无二的值。它是JavaScript 语言的第七种数据类型，是一种类似于字符串的数据类型。</p>
<h5 id="特点"><a href="#特点" class="headerlink" title="特点"></a>特点</h5><ul>
<li>Symbol 的值是唯一的，用来解决命名冲突的问题</li>
<li>Symbol 值不能与其他数据进行运算</li>
<li>Symbol 定义的 对象属性 不能使用 for…in 循环遍历 ，但是可以 使用 Reflect.ownKeys 来获取对象的所有键名</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs js"><span class="hljs-comment">// 创建 Symbol</span><br><span class="hljs-keyword">let</span> s = <span class="hljs-built_in">Symbol</span>();<br><span class="hljs-built_in">console</span>.log(s, <span class="hljs-keyword">typeof</span> s); <span class="hljs-comment">// Symbol() &quot;symbol&quot;</span><br><span class="hljs-keyword">let</span> s2 = <span class="hljs-built_in">Symbol</span>(<span class="hljs-string">&#x27;张艺兴&#x27;</span>); <span class="hljs-comment">// 注释作用</span><br><span class="hljs-keyword">let</span> s3 = <span class="hljs-built_in">Symbol</span>(<span class="hljs-string">&#x27;张艺兴&#x27;</span>);<br><span class="hljs-built_in">console</span>.log(s2 === s3); <span class="hljs-comment">// false</span><br><span class="hljs-comment">// Symbol.for 创建</span><br><span class="hljs-keyword">let</span> s4 = <span class="hljs-built_in">Symbol</span>.for(<span class="hljs-string">&#x27;张艺兴&#x27;</span>);<br><span class="hljs-keyword">let</span> s5 = <span class="hljs-built_in">Symbol</span>.for(<span class="hljs-string">&#x27;张艺兴&#x27;</span>);<br><span class="hljs-built_in">console</span>.log(s4, <span class="hljs-keyword">typeof</span> s4); <span class="hljs-comment">// Symbol(张艺兴) &quot;symbol&quot;</span><br><span class="hljs-built_in">console</span>.log(s4 === s5); <span class="hljs-comment">// true</span><br></code></pre></div></td></tr></table></figure>

<p><code>Symbol[&#39;say&#39;]: function () &#123;&#125;</code></p>
<h5 id="内置值"><a href="#内置值" class="headerlink" title="内置值"></a>内置值</h5><h4 id="JS-数据类型总结"><a href="#JS-数据类型总结" class="headerlink" title="JS 数据类型总结"></a>JS 数据类型总结</h4><p><strong>USONB</strong> (you are so niubility)</p>
<ul>
<li>u : undefined</li>
<li>s : string Symbol</li>
<li>o : object</li>
<li>n : null number</li>
<li>b : boolean</li>
</ul>
<h3 id="2-10-迭代器"><a href="#2-10-迭代器" class="headerlink" title="2.10 迭代器"></a>2.10 迭代器</h3><p>遍历器（Iterator）就是一种机制。它是一种接口，为各种不同的数据结构提供统一的访问机制。任何数据结构只要部署 Iterator 接口，就可以完成遍历操作。</p>
<ul>
<li><p>ES6 创造了一种新的遍历命令 for…of 循环，Iterator 接口主要供 for…of 消费</p>
<figure class="highlight js"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs js"><span class="hljs-comment">// 声明一个数组</span><br><span class="hljs-keyword">const</span> xiyou = [<span class="hljs-string">&#x27;1&#x27;</span>, <span class="hljs-string">&#x27;2&#x27;</span>, <span class="hljs-string">&#x27;3&#x27;</span>, <span class="hljs-string">&#x27;4&#x27;</span>];<br><br><span class="hljs-comment">// 使用 for...of 遍历数组</span><br><span class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> v <span class="hljs-keyword">of</span> xiyou) &#123;<br>    <span class="hljs-built_in">console</span>.log(v);<br>&#125;<br></code></pre></div></td></tr></table></figure></li>
<li><p>原生具备 iterator 接口的数据(可用 for of 遍历)</p>
<ul>
<li>Array</li>
<li>Arguments</li>
<li>Set</li>
<li>Map</li>
<li>String</li>
<li>TypedArray</li>
<li>NodeList</li>
</ul>
</li>
<li><p>工作原理</p>
<ul>
<li>创建一个指针对象，指向当前数据结构的起始位置</li>
<li>第一次调用对象的 next 方法，指针自动指向数据结构的第一个成员</li>
<li>接下来不断调用 next 方法，指针一直往后移动，直到指向最后一个成员</li>
<li>每调用 next 方法返回一个包含 value 和 done 属性的对象</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs js"><span class="hljs-keyword">let</span> iterator = xiyou[<span class="hljs-built_in">Symbol</span>.iterator]();<br><br><span class="hljs-comment">// 调用对象的 next 方法</span><br><span class="hljs-built_in">console</span>.log(iterator.next());<br><span class="hljs-built_in">console</span>.log(iterator.next());<br><span class="hljs-built_in">console</span>.log(iterator.next());<br><span class="hljs-built_in">console</span>.log(iterator.next());<br><span class="hljs-built_in">console</span>.log(iterator.next());<br></code></pre></div></td></tr></table></figure>

<p>运行结果：</p>
<p><img src="/2021/07/30/ES6%E7%AC%94%E8%AE%B0/image-20210622185251823.png" srcset="/img/loading2.gif" lazyload></p>
</li>
</ul>
<h5 id="应用场景-4"><a href="#应用场景-4" class="headerlink" title="应用场景"></a>应用场景</h5><p>需要自定义遍历数据的时候，要想到迭代器。</p>
<h5 id="自定义遍历对象"><a href="#自定义遍历对象" class="headerlink" title="自定义遍历对象"></a>自定义遍历对象</h5><figure class="highlight js"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs js"><span class="hljs-comment">// 声明一个对象</span><br><span class="hljs-keyword">const</span> banji = &#123;<br>    <span class="hljs-attr">name</span>: <span class="hljs-string">&#x27;终极一班&#x27;</span>,<br>    <span class="hljs-attr">stus</span>: [<br>        <span class="hljs-string">&#x27;xiaoming&#x27;</span>,<br>        <span class="hljs-string">&#x27;xiaoning&#x27;</span>,<br>        <span class="hljs-string">&#x27;xiaotian&#x27;</span>,<br>        <span class="hljs-string">&#x27;knight&#x27;</span><br>    ],<br>    [<span class="hljs-built_in">Symbol</span>.iterator]() &#123;<br>        <span class="hljs-comment">// 索引变量</span><br>        <span class="hljs-keyword">let</span> index = <span class="hljs-number">0</span>;<br>        <span class="hljs-keyword">let</span> _this = <span class="hljs-built_in">this</span>;<br>        <span class="hljs-keyword">return</span> &#123;<br>            <span class="hljs-attr">next</span>: <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>&#123;<br>                <span class="hljs-keyword">if</span> (index &lt; _this.stus.length) &#123;<br>                    <span class="hljs-keyword">const</span> result = &#123;<span class="hljs-attr">value</span>: _this.stus[index], <span class="hljs-attr">done</span>: <span class="hljs-literal">false</span>&#125;<br>                    <span class="hljs-comment">// 下表自增</span><br>                    index++;<br>                    <span class="hljs-comment">// 返回结果</span><br>                    <span class="hljs-keyword">return</span> result<br>                &#125; <span class="hljs-keyword">else</span> &#123;<br>                    <span class="hljs-keyword">return</span> &#123;<span class="hljs-attr">value</span>: <span class="hljs-literal">undefined</span>, <span class="hljs-attr">done</span>: <span class="hljs-literal">true</span>&#125;<br>                &#125;<br>            &#125;<br>        &#125;<br>    &#125;<br>&#125;<br><br><span class="hljs-comment">// 遍历这个对象</span><br><span class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> v <span class="hljs-keyword">of</span> banji) &#123;<br>    <span class="hljs-built_in">console</span>.log(v);<br>&#125;<br></code></pre></div></td></tr></table></figure>

<h3 id="2-11-生成器"><a href="#2-11-生成器" class="headerlink" title="2.11 生成器"></a>2.11 生成器</h3><p>生成器函数是 ES6 提供的一种异步编程解决方案，语法行为与传统函数完全不同，起其实就是一个特殊的函数。</p>
<figure class="highlight js"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs js"><span class="hljs-comment">// yield 函数代码的分隔符</span><br><span class="hljs-function"><span class="hljs-keyword">function</span> * <span class="hljs-title">gen</span>(<span class="hljs-params"></span>) </span>&#123;<br>    <span class="hljs-comment">// console.log(111);</span><br>    <span class="hljs-keyword">yield</span> <span class="hljs-string">&#x27;一只没有耳朵&#x27;</span>;<br>    <span class="hljs-comment">// console.log(222);</span><br>    <span class="hljs-keyword">yield</span> <span class="hljs-string">&#x27;一只没有尾巴&#x27;</span>;<br>    <span class="hljs-comment">// console.log(333);</span><br>    <span class="hljs-keyword">yield</span> <span class="hljs-string">&#x27;真奇怪&#x27;</span>;<br>    <span class="hljs-comment">// console.log(444);</span><br>&#125;<br><br><span class="hljs-keyword">let</span> iterator = gen();<br><span class="hljs-comment">// 调用 next() 方法才会执行方法</span><br><span class="hljs-built_in">console</span>.log(iterator.next());<br><span class="hljs-built_in">console</span>.log(iterator.next());<br><span class="hljs-built_in">console</span>.log(iterator.next());<br><span class="hljs-built_in">console</span>.log(iterator.next());<br><br><span class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> v <span class="hljs-keyword">of</span> gen()) &#123;<br>    <span class="hljs-built_in">console</span>.log(v);<br>&#125;<br></code></pre></div></td></tr></table></figure>

<p>运行截图：</p>
<p><img src="/2021/07/30/ES6%E7%AC%94%E8%AE%B0/image-20210622193807793.png" srcset="/img/loading2.gif" lazyload></p>
<h5 id="生成器函数参数"><a href="#生成器函数参数" class="headerlink" title="生成器函数参数"></a>生成器函数参数</h5><figure class="highlight js"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs js"><span class="hljs-function"><span class="hljs-keyword">function</span> * <span class="hljs-title">gen</span>(<span class="hljs-params">arg</span>) </span>&#123;<br>    <span class="hljs-built_in">console</span>.log(arg);<br>    <span class="hljs-keyword">let</span> one = <span class="hljs-keyword">yield</span> <span class="hljs-string">&#x27;一只没有耳朵&#x27;</span>;<br>    <span class="hljs-built_in">console</span>.log(one);<br>    <span class="hljs-keyword">let</span> two = <span class="hljs-keyword">yield</span> <span class="hljs-string">&#x27;一只没有尾巴&#x27;</span>;<br>    <span class="hljs-built_in">console</span>.log(two);<br>    <span class="hljs-keyword">let</span> three = <span class="hljs-keyword">yield</span> <span class="hljs-string">&#x27;真奇怪&#x27;</span>;<br>    <span class="hljs-built_in">console</span>.log(three);<br>&#125;<br><br><span class="hljs-keyword">let</span> iterator = gen(<span class="hljs-string">&#x27;AAA&#x27;</span>);<br><span class="hljs-comment">// 调用 next() 方法才会执行方法</span><br><span class="hljs-built_in">console</span>.log(iterator.next());<br><span class="hljs-built_in">console</span>.log(iterator.next(<span class="hljs-string">&#x27;BBB&#x27;</span>));<br><span class="hljs-built_in">console</span>.log(iterator.next(<span class="hljs-string">&#x27;CCC&#x27;</span>));<br><span class="hljs-built_in">console</span>.log(iterator.next(<span class="hljs-string">&#x27;DDD&#x27;</span>));<br></code></pre></div></td></tr></table></figure>

<p>运行截图：</p>
<p><img src="/2021/07/30/ES6%E7%AC%94%E8%AE%B0/image-20210622194527052.png" srcset="/img/loading2.gif" lazyload></p>
<h5 id="实例"><a href="#实例" class="headerlink" title="实例"></a>实例</h5><p>…</p>
<h3 id="2-12-Promise"><a href="#2-12-Promise" class="headerlink" title="2.12 Promise"></a>2.12 Promise</h3><p>Promise 是 ES6 引入的异步编程的新解决方案。语法上 Promise 是一个构造函数，用来封装异步操作并可以获取其成功或失败的结果。</p>
<h5 id="Promise-构造方法"><a href="#Promise-构造方法" class="headerlink" title="Promise 构造方法"></a>Promise 构造方法</h5><p><code>const promise = new Promise(function(resolve, reject) &#123;&#125;</code></p>
<figure class="highlight js"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs js"><span class="hljs-comment">// 实例化 Promise 对象</span><br><span class="hljs-keyword">const</span> p = <span class="hljs-keyword">new</span> <span class="hljs-built_in">Promise</span>(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">resolve, reject</span>) </span>&#123;<br>    <span class="hljs-built_in">setTimeout</span>(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>&#123;<br>        <span class="hljs-comment">// let data = &#x27;数据库中的用户数据&#x27;</span><br>        <span class="hljs-comment">// resolve(data); // 调用 then 中第一个方法</span><br><br>        <span class="hljs-keyword">let</span> err = <span class="hljs-string">&#x27;数据读取失败&#x27;</span>;<br>        reject(err); <span class="hljs-comment">// 调用 then 中第二个方法</span><br>    &#125;, <span class="hljs-number">1000</span>);<br>&#125;);<br><br><span class="hljs-comment">// 调用 Promise 对象的 then 方法</span><br>p.then(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">value</span>) </span>&#123;<br>    <span class="hljs-built_in">console</span>.log(value); <span class="hljs-comment">// 数据库中的用户数据</span><br>&#125;, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">reason</span>) </span>&#123;<br>    <span class="hljs-built_in">console</span>.log(reason); <span class="hljs-comment">// 数据读取失败</span><br>&#125;)<br></code></pre></div></td></tr></table></figure>

<h5 id="Promise-读取文件"><a href="#Promise-读取文件" class="headerlink" title="Promise 读取文件"></a>Promise 读取文件</h5><figure class="highlight js"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs js"><span class="hljs-comment">// 引入 fs 模块</span><br><span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class="hljs-string">&#x27;fs&#x27;</span>);<br><br><span class="hljs-keyword">const</span> p = <span class="hljs-keyword">new</span> <span class="hljs-built_in">Promise</span>(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">resolve, reject</span>) </span>&#123;<br>    fs.readFile(<span class="hljs-string">&#x27;./resources/为学.md&#x27;</span>, <span class="hljs-string">&#x27;utf8&#x27;</span>, <span class="hljs-function">(<span class="hljs-params">err, data</span>) =&gt;</span> &#123;<br>        <span class="hljs-keyword">if</span> (err) reject(err);<br>        <span class="hljs-comment">// 如果成功</span><br>        resolve(data);<br>    &#125;)<br>&#125;)<br><br>p.then(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">value</span>) </span>&#123;<br>    <span class="hljs-built_in">console</span>.log(value);<br>&#125;, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">reason</span>) </span>&#123;<br>    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&#x27;读取失败&#x27;</span>);<br>&#125;)<br></code></pre></div></td></tr></table></figure>

<h5 id="Promise-封装-AJAX"><a href="#Promise-封装-AJAX" class="headerlink" title="Promise 封装 AJAX"></a>Promise 封装 AJAX</h5><figure class="highlight js"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs js"><span class="hljs-comment">// 接口地址 http://api.apiopen.top/getJoke</span><br><br><span class="hljs-keyword">const</span> p = <span class="hljs-keyword">new</span> <span class="hljs-built_in">Promise</span>(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">resolve, reject</span>) </span>&#123;<br>    <span class="hljs-comment">// 1. 创建对象</span><br>    <span class="hljs-keyword">const</span> xhr = <span class="hljs-keyword">new</span> XMLHttpRequest();<br><br>    <span class="hljs-comment">// 2. 初始化</span><br>    xhr.open(<span class="hljs-string">&#x27;GET&#x27;</span>, <span class="hljs-string">&#x27;https://api.apiopen.top/getJoke&#x27;</span>);<br><br>    <span class="hljs-comment">// 3. 发送</span><br>    xhr.send();<br><br>    <span class="hljs-comment">// 4. 绑定事件，处理响应结果</span><br>    xhr.onreadystatechange = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>&#123;<br>        <span class="hljs-keyword">if</span> (xhr.readyState === <span class="hljs-number">4</span>) &#123;<br>            <span class="hljs-keyword">if</span> (xhr.status &gt;= <span class="hljs-number">200</span> &amp;&amp; xhr.status &lt; <span class="hljs-number">300</span>) &#123;<br>                resolve(xhr.response)<br>            &#125; <span class="hljs-keyword">else</span> &#123;<br>                reject()<br>            &#125;<br>        &#125;<br>    &#125;<br>&#125;)<br><br><span class="hljs-comment">// 指定回调</span><br>p.then(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">value</span>) </span>&#123;<br>    <span class="hljs-built_in">console</span>.log(value);<br>&#125;, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">reason</span>) </span>&#123;<br>    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&#x27;获取失败&#x27;</span>);<br>&#125;)<br></code></pre></div></td></tr></table></figure>

<h5 id="Promise-prototype-then"><a href="#Promise-prototype-then" class="headerlink" title="Promise.prototype.then"></a>Promise.prototype.then</h5><figure class="highlight js"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs js"><span class="hljs-comment">// 创建 promise 对象</span><br><span class="hljs-keyword">const</span> p = <span class="hljs-keyword">new</span> <span class="hljs-built_in">Promise</span>(<span class="hljs-function">(<span class="hljs-params">resolve, reject</span>) =&gt;</span> &#123;<br>    <span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =&gt;</span> &#123;<br>        resolve(<span class="hljs-string">&#x27;用户数据&#x27;</span>);<br>        <span class="hljs-comment">// reject(&#x27;出错啦&#x27;);</span><br>    &#125;, <span class="hljs-number">1000</span>);<br>&#125;);<br><br><span class="hljs-comment">// 调用 then 方法  then方法的返回结果是 Promise 对象，对象状态又回调函数的执行结果决定</span><br><span class="hljs-comment">// 1. 如果回调函数中返回的结果是一个 非Promise 类型的数据，状态为成功，返回值为对象那个的成功值</span><br><span class="hljs-keyword">const</span> result = p.then(<span class="hljs-function"><span class="hljs-params">value</span> =&gt;</span> &#123;<br>    <span class="hljs-built_in">console</span>.log(value);<br>    <span class="hljs-comment">// 1. 非 Promise 类型的数据</span><br>    <span class="hljs-comment">// return 123;</span><br>    <span class="hljs-comment">// 2. 是 Promise 对象</span><br>    <span class="hljs-comment">// return new Promise((resolve, reject) =&gt; &#123;</span><br>    <span class="hljs-comment">//   // resolve(&#x27;ok&#x27;);</span><br>    <span class="hljs-comment">//   reject(&#x27;出错&#x27;)</span><br>    <span class="hljs-comment">// &#125;)</span><br>    <span class="hljs-comment">// 3. 抛出错误</span><br>    <span class="hljs-comment">// throw new Error(&#x27;出错啦&#x27;)</span><br>&#125;, <span class="hljs-function"><span class="hljs-params">reason</span> =&gt;</span> &#123;<br>    <span class="hljs-built_in">console</span>.warn(reason);<br>&#125;);<br><span class="hljs-built_in">console</span>.log(result);<br></code></pre></div></td></tr></table></figure>

<h6 id="链式调用"><a href="#链式调用" class="headerlink" title="链式调用"></a>链式调用</h6><figure class="highlight js"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs js"><span class="hljs-comment">// 链式调用 可以避免回调地狱的形成</span><br>p.then(<span class="hljs-function"><span class="hljs-params">value</span>=&gt;</span>&#123;<br><br>&#125;,<span class="hljs-function"><span class="hljs-params">reason</span>=&gt;</span>&#123;<br><br>&#125;).then(<span class="hljs-function"><span class="hljs-params">value</span>=&gt;</span>&#123;<br><br>&#125;)<br></code></pre></div></td></tr></table></figure>

<p>另外，then 方法中的两个方法 不一定需要全有</p>
<h6 id="案例-读取多个文件"><a href="#案例-读取多个文件" class="headerlink" title="案例-读取多个文件"></a>案例-读取多个文件</h6><figure class="highlight js"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs js"><span class="hljs-comment">// 引入 fs 模块</span><br><span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class="hljs-string">&#x27;fs&#x27;</span>);<br><br><span class="hljs-comment">// 之前的方法 - 回调地狱</span><br><span class="hljs-comment">// fs.readFile(&#x27;./resources/为学.md&#x27;, &#x27;utf8&#x27;, (err, data1) =&gt; &#123;</span><br><span class="hljs-comment">//   fs.readFile(&#x27;./resources/插秧诗.md&#x27;, &#x27;utf8&#x27;, (err, data2) =&gt; &#123;</span><br><span class="hljs-comment">//     fs.readFile(&#x27;./resources/观书有感.md&#x27;, &#x27;utf8&#x27;, (err, data3) =&gt; &#123;</span><br><span class="hljs-comment">//       let result = data1 + &#x27;\r\n&#x27; + data2 + &#x27;\r\n&#x27; + data3;</span><br><span class="hljs-comment">//       console.log(result);</span><br><span class="hljs-comment">//     &#125;)</span><br><span class="hljs-comment">//   &#125;)</span><br><span class="hljs-comment">// &#125;)</span><br><br><span class="hljs-comment">// 使用 Promise 实现</span><br><span class="hljs-keyword">const</span> p = <span class="hljs-keyword">new</span> <span class="hljs-built_in">Promise</span>(<span class="hljs-function">(<span class="hljs-params">resolve, reject</span>) =&gt;</span> &#123;<br>    fs.readFile(<span class="hljs-string">&#x27;./resources/为学.md&#x27;</span>, <span class="hljs-string">&#x27;utf8&#x27;</span>, <span class="hljs-function">(<span class="hljs-params">err, data</span>) =&gt;</span> &#123;<br>        resolve(data);<br>    &#125;)<br>&#125;)<br><br>p.then(<span class="hljs-function"><span class="hljs-params">value</span> =&gt;</span> &#123;<br>    <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-built_in">Promise</span>(<span class="hljs-function">(<span class="hljs-params">resolve, reject</span>) =&gt;</span> &#123;<br>        fs.readFile(<span class="hljs-string">&#x27;./resources/插秧诗.md&#x27;</span>, <span class="hljs-string">&#x27;utf8&#x27;</span>, <span class="hljs-function">(<span class="hljs-params">err, data</span>) =&gt;</span> &#123;<br>            resolve([value, data]);<br>        &#125;)<br>    &#125;)<br>&#125;).then(<span class="hljs-function"><span class="hljs-params">value</span> =&gt;</span> &#123;<br>    <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-built_in">Promise</span>(<span class="hljs-function">(<span class="hljs-params">resolve, reject</span>) =&gt;</span> &#123;<br>        fs.readFile(<span class="hljs-string">&#x27;./resources/观书有感.md&#x27;</span>, <span class="hljs-string">&#x27;utf8&#x27;</span>, <span class="hljs-function">(<span class="hljs-params">err, data</span>) =&gt;</span> &#123;<br>            value.push(data);<br>            resolve(value);<br>        &#125;)<br>    &#125;)<br>&#125;).then(<span class="hljs-function"><span class="hljs-params">value</span> =&gt;</span> &#123;<br>    <span class="hljs-built_in">console</span>.log(value.join(<span class="hljs-string">&#x27;\r\n&#x27;</span>));<br>&#125;)<br></code></pre></div></td></tr></table></figure>

<h5 id="Promise-catch-方法"><a href="#Promise-catch-方法" class="headerlink" title="Promise catch 方法"></a>Promise catch 方法</h5><p>不用不影响开发，catch 的作用只是简化，可以只写后面错误的函数，不需要写前面的。</p>
<figure class="highlight js"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs js"><span class="hljs-keyword">const</span> p = <span class="hljs-keyword">new</span> <span class="hljs-built_in">Promise</span>(<span class="hljs-function">(<span class="hljs-params">resolve, reject</span>) =&gt;</span> &#123;<br>    <span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =&gt;</span> &#123;<br>        <span class="hljs-comment">// 设置 p 对象的状态为失败，并设置失败的值</span><br>        reject(<span class="hljs-string">&#x27;出错啦！&#x27;</span>)<br>    &#125;, <span class="hljs-number">1000</span>);<br>&#125;)<br><br>p.then(<span class="hljs-function"><span class="hljs-params">value</span> =&gt;</span> &#123;&#125;, <span class="hljs-function"><span class="hljs-params">reason</span> =&gt;</span> &#123;<br>    <span class="hljs-built_in">console</span>.error(reason);<br>&#125;)<br><br>p.catch(<span class="hljs-function"><span class="hljs-params">reason</span> =&gt;</span> &#123;<br>    <span class="hljs-built_in">console</span>.warn(reason);<br>&#125;)<br></code></pre></div></td></tr></table></figure>

<h3 id="2-13-Set"><a href="#2-13-Set" class="headerlink" title="2.13 Set"></a>2.13 Set</h3><p>ES6 提供了新的数据结构 Set（集合）。</p>
<p>它类似于数组，但成员的值都是唯一的，集合实现了 iterator 接口，所以可以使用『扩展运算符』和『for…of…』进行遍历。</p>
<h5 id="属性-amp-方法"><a href="#属性-amp-方法" class="headerlink" title="属性&amp;方法"></a>属性&amp;方法</h5><ul>
<li>size     返回集合的元素个数</li>
<li>add     增加一个新元素，返回当前集合</li>
<li>delete 删除元素，返回 boolean 值</li>
<li>has      检测集合中是否包含某个元素，返回 boolean 值</li>
<li>clear    清空集合，返回 undefined</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs js"><span class="hljs-comment">// 声明一个 Set</span><br><span class="hljs-keyword">let</span> s = <span class="hljs-keyword">new</span> <span class="hljs-built_in">Set</span>();<br><span class="hljs-keyword">let</span> s2 = <span class="hljs-keyword">new</span> <span class="hljs-built_in">Set</span>([<span class="hljs-string">&#x27;大事儿&#x27;</span>, <span class="hljs-string">&#x27;小事儿&#x27;</span>, <span class="hljs-string">&#x27;好事儿&#x27;</span>, <span class="hljs-string">&#x27;坏事儿&#x27;</span>, <span class="hljs-string">&#x27;小事儿&#x27;</span>]);<br><span class="hljs-built_in">console</span>.log(s, <span class="hljs-keyword">typeof</span> s);<br><span class="hljs-built_in">console</span>.log(s2); <span class="hljs-comment">// 自动去重 &#123;&quot;大事儿&quot;, &quot;小事儿&quot;, &quot;好事儿&quot;, &quot;坏事儿&quot;&#125;</span><br><br><span class="hljs-comment">// 元素的个数</span><br><span class="hljs-built_in">console</span>.log(s2.size); <span class="hljs-comment">// 4</span><br><span class="hljs-comment">// 添加新的元素</span><br>s2.add(<span class="hljs-string">&#x27;喜事儿&#x27;</span>);<br><span class="hljs-built_in">console</span>.log(s2); <span class="hljs-comment">// &#123;&quot;大事儿&quot;, &quot;小事儿&quot;, &quot;好事儿&quot;, &quot;坏事儿&quot;, &quot;喜事儿&quot;&#125;</span><br><span class="hljs-comment">// 删除元素</span><br>s2.delete(<span class="hljs-string">&#x27;坏事儿&#x27;</span>);<br><span class="hljs-built_in">console</span>.log(s2); <span class="hljs-comment">// &#123;&quot;大事儿&quot;, &quot;小事儿&quot;, &quot;好事儿&quot;, &quot;喜事儿&quot;&#125;</span><br><span class="hljs-comment">// 检测</span><br><span class="hljs-built_in">console</span>.log(s2.has(<span class="hljs-string">&#x27;好事儿&#x27;</span>)); <span class="hljs-comment">// true</span><br><span class="hljs-comment">// 清空</span><br>s2.clear();<br><span class="hljs-built_in">console</span>.log(s2); <span class="hljs-comment">// &#123;&#125;</span><br><br><span class="hljs-comment">// 也可实现 for...of 遍历</span><br><span class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> v <span class="hljs-keyword">of</span> s2) &#123;<br>    <span class="hljs-built_in">console</span>.log(v);<br>&#125;<br></code></pre></div></td></tr></table></figure>

<h5 id="Set-集合实践"><a href="#Set-集合实践" class="headerlink" title="Set 集合实践"></a>Set 集合实践</h5><figure class="highlight js"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs js"><span class="hljs-keyword">var</span> arr = [<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>,<span class="hljs-number">4</span>,<span class="hljs-number">5</span>,<span class="hljs-number">4</span>,<span class="hljs-number">3</span>,<span class="hljs-number">2</span>,<span class="hljs-number">1</span>];<br><span class="hljs-comment">// 1. 数组去重</span><br><span class="hljs-comment">// let result = [...new Set(arr)];</span><br><span class="hljs-comment">// console.log(result);</span><br><span class="hljs-comment">// 2. 交集</span><br><span class="hljs-keyword">let</span> arr2 = [<span class="hljs-number">4</span>,<span class="hljs-number">5</span>,<span class="hljs-number">6</span>,<span class="hljs-number">5</span>,<span class="hljs-number">6</span>];<br><span class="hljs-comment">// let result = [...new Set(arr)].filter(item =&gt; &#123;</span><br><span class="hljs-comment">//   let s2 = new Set(arr2);</span><br><span class="hljs-comment">//   if (s2.has(item)) &#123;</span><br><span class="hljs-comment">//     return true;</span><br><span class="hljs-comment">//   &#125; else &#123;</span><br><span class="hljs-comment">//     return false;</span><br><span class="hljs-comment">//   &#125;</span><br><span class="hljs-comment">// &#125;)</span><br><span class="hljs-comment">// 简化</span><br><span class="hljs-keyword">let</span> result = [...new <span class="hljs-built_in">Set</span>(arr)].filter(<span class="hljs-function"><span class="hljs-params">item</span> =&gt;</span> <span class="hljs-keyword">new</span> <span class="hljs-built_in">Set</span>(arr2).has(item))<br><span class="hljs-built_in">console</span>.log(result); <span class="hljs-comment">// [4, 5]</span><br><br><span class="hljs-comment">// 3. 并集</span><br><span class="hljs-keyword">let</span> union = [...new <span class="hljs-built_in">Set</span>([...arr, ...arr2])]<br><span class="hljs-built_in">console</span>.log(union);  <span class="hljs-comment">// [1, 2, 3, 4, 5, 6]</span><br><br><span class="hljs-comment">// 4. 差集</span><br><span class="hljs-keyword">let</span> diff = [...new <span class="hljs-built_in">Set</span>(arr)].filter(<span class="hljs-function"><span class="hljs-params">item</span> =&gt;</span> !(<span class="hljs-keyword">new</span> <span class="hljs-built_in">Set</span>(arr2).has(item)))<br><span class="hljs-built_in">console</span>.log(diff);   <span class="hljs-comment">// [1, 2, 3]</span><br></code></pre></div></td></tr></table></figure>

<h3 id="2-14-Map"><a href="#2-14-Map" class="headerlink" title="2.14 Map"></a>2.14 Map</h3><p>ES6 提供了 Map 数据结构。它类似于对象，也是键值对的集合。但是“键”<br>的范围不限于字符串，各种类型的值（包括对象）都可以当作键。Map 也实现了iterator 接口，所以可以使用『扩展运算符』和『for…of…』进行遍历。</p>
<h5 id="属性-amp-方法-1"><a href="#属性-amp-方法-1" class="headerlink" title="属性&amp;方法"></a>属性&amp;方法</h5><ul>
<li>size     返回 Map 的元素个数</li>
<li>add     增加一个新元素，返回当前 Map</li>
<li>get      返回键名对象的键值</li>
<li>has      检测 Map 中是否包含某个元素，返回 boolean 值</li>
<li>clear    清空集合，返回 undefined</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs js"><span class="hljs-comment">// 声明 Map</span><br><span class="hljs-keyword">let</span> m = <span class="hljs-keyword">new</span> <span class="hljs-built_in">Map</span>();<br><br><span class="hljs-comment">// 添加元素</span><br>m.set(<span class="hljs-string">&#x27;name&#x27;</span>, <span class="hljs-string">&#x27;张艺兴&#x27;</span>)<br>m.set(<span class="hljs-string">&#x27;change&#x27;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>&#123;<br>    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&#x27;张艺兴是最帅的&#x27;</span>);<br>&#125;)<br><span class="hljs-keyword">let</span> key = &#123;<br>    <span class="hljs-attr">school</span> : <span class="hljs-string">&#x27;AgoniLay&#x27;</span><br>&#125;<br>m.set(key, [<span class="hljs-string">&#x27;北京&#x27;</span>, <span class="hljs-string">&#x27;上海&#x27;</span>, <span class="hljs-string">&#x27;深圳&#x27;</span>])<br><br><span class="hljs-comment">// size</span><br><span class="hljs-built_in">console</span>.log(m.size); <span class="hljs-comment">// 3</span><br><br><span class="hljs-comment">// 删除</span><br>m.delete(<span class="hljs-string">&#x27;name&#x27;</span>)<br><br><span class="hljs-comment">// 获取</span><br><span class="hljs-comment">// console.log(m.get(&#x27;change&#x27;));</span><br><span class="hljs-built_in">console</span>.log(m.get(key)); <span class="hljs-comment">// [&quot;北京&quot;, &quot;上海&quot;, &quot;深圳&quot;]</span><br><br><span class="hljs-comment">// 清空</span><br><span class="hljs-comment">// m.clear();</span><br><br><span class="hljs-comment">// 遍历</span><br><span class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> v <span class="hljs-keyword">of</span> m) &#123;<br>    <span class="hljs-built_in">console</span>.log(v);<br>&#125;<br><br><span class="hljs-built_in">console</span>.log(m);<br></code></pre></div></td></tr></table></figure>

<h3 id="2-15-Class-类"><a href="#2-15-Class-类" class="headerlink" title="2.15 Class 类"></a>2.15 Class 类</h3><p>ES6 提供了更接近传统语言的写法，引入了 Class（类）这个概念，作为对<br>象的模板。通过 class 关键字，可以定义类。基本上，ES6 的 class 可以看作只是一个语法糖，它的绝大部分功能，ES5 都可以做到，新的 class 写法只是让对象原型的写法更加清晰、更像面向对象编程的语法而已。</p>
<h5 id="知识点"><a href="#知识点" class="headerlink" title="知识点"></a>知识点</h5><ul>
<li>class 声明类</li>
<li>constructor 定义构造函数初始化</li>
<li>extends 继承父类</li>
<li>super 调用父级构造方法</li>
<li>static 定义静态方法和属性</li>
<li>父类方法可以重写</li>
</ul>
<h5 id="ES5-方法"><a href="#ES5-方法" class="headerlink" title="ES5 方法"></a>ES5 方法</h5><figure class="highlight js"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs js"><span class="hljs-comment">// ES5</span><br><span class="hljs-comment">// 手机</span><br><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Phone</span>(<span class="hljs-params">brand, price</span>) </span>&#123;<br>    <span class="hljs-built_in">this</span>.brand = brand;<br>    <span class="hljs-built_in">this</span>.price = price;<br>&#125;<br><br><span class="hljs-comment">// 添加方法</span><br>Phone.prototype.call = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>&#123;<br>    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&#x27;我可以打电话&#x27;</span>);<br>&#125;<br><br><span class="hljs-comment">// 实例化对象</span><br><span class="hljs-keyword">let</span> huawei = <span class="hljs-keyword">new</span> Phone(<span class="hljs-string">&#x27;华为&#x27;</span>, <span class="hljs-number">5999</span>);<br><br><span class="hljs-built_in">console</span>.log(huawei);<br><span class="hljs-comment">// huawei.call();</span><br></code></pre></div></td></tr></table></figure>

<h5 id="ES6-class"><a href="#ES6-class" class="headerlink" title="ES6 class"></a>ES6 class</h5><figure class="highlight js"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs js"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Shouji</span> </span>&#123;<br>    <span class="hljs-comment">// 构造方法 名字不能修改</span><br>    <span class="hljs-title">constructor</span> (<span class="hljs-params">brand, price</span>) &#123;<br>        <span class="hljs-built_in">this</span>.brand = brand;<br>        <span class="hljs-built_in">this</span>.price = price;<br>    &#125;<br><br>    <span class="hljs-comment">// 方法必须使用该语法，不能使用 ES5 的对象完整形式</span><br>    call () &#123;<br>        <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&#x27;我可以打电话&#x27;</span>);<br>    &#125;<br>&#125;<br><br><span class="hljs-keyword">let</span> onePlus = <span class="hljs-keyword">new</span> Shouji(<span class="hljs-string">&#x27;1+&#x27;</span>, <span class="hljs-number">1999</span>);<br><span class="hljs-built_in">console</span>.log(onePlus);<br></code></pre></div></td></tr></table></figure>

<h5 id="静态成员"><a href="#静态成员" class="headerlink" title="静态成员"></a>静态成员</h5><figure class="highlight js"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs js"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Phone</span> </span>&#123;<br>    <span class="hljs-comment">// 静态属性</span><br>    <span class="hljs-keyword">static</span> name = <span class="hljs-string">&#x27;手机&#x27;</span>;<br><span class="hljs-keyword">static</span> change = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>&#123;<br>    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&#x27;我可以改变世界&#x27;</span>);<br>&#125;;<br>&#125;<br><br><span class="hljs-keyword">let</span> nokia = <span class="hljs-keyword">new</span> Phone();<br><span class="hljs-built_in">console</span>.log(nokia.name); <span class="hljs-comment">// undefined</span><br><span class="hljs-built_in">console</span>.log(Phone.name); <span class="hljs-comment">// 手机</span><br></code></pre></div></td></tr></table></figure>

<h5 id="类继承"><a href="#类继承" class="headerlink" title="类继承"></a>类继承</h5><figure class="highlight js"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs js"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Phone</span> </span>&#123;<br>    <span class="hljs-comment">// 构造方法</span><br>    <span class="hljs-title">constructor</span> (<span class="hljs-params">brand, price</span>) &#123;<br>        <span class="hljs-built_in">this</span>.brand = brand;<br>        <span class="hljs-built_in">this</span>.price = price;<br>    &#125;<br><br>    <span class="hljs-comment">// 父类的成员属性</span><br>    call () &#123;<br>        <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&#x27;我可以打电话&#x27;</span>);<br>    &#125;<br>&#125;<br><br><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">SmartPhone</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Phone</span> </span>&#123;<br>    <span class="hljs-comment">// 构造方法</span><br>    <span class="hljs-title">constructor</span> (<span class="hljs-params">brand, price, color, size</span>) &#123;<br>        <span class="hljs-built_in">super</span>(brand, price);<br>        <span class="hljs-built_in">this</span>.color = color;<br>        <span class="hljs-built_in">this</span>.size = size;<br>    &#125;<br><br>    photo () &#123;<br>        <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&#x27;拍照&#x27;</span>);<br>    &#125;<br><br>    playGame () &#123;<br>        <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&#x27;打游戏&#x27;</span>);<br>    &#125;<br>&#125;<br><br><span class="hljs-keyword">const</span> xiaomi = <span class="hljs-keyword">new</span> SmartPhone(<span class="hljs-string">&#x27;小米&#x27;</span>, <span class="hljs-number">799</span>, <span class="hljs-string">&#x27;黑色&#x27;</span>, <span class="hljs-string">&#x27;4.7inch&#x27;</span>);<br><span class="hljs-built_in">console</span>.log(xiaomi);<br>xiaomi.call();<br>xiaomi.photo();<br>xiaomi.playGame();<br></code></pre></div></td></tr></table></figure>

<p>子类可以进行对父类方法的重写</p>
<p>注：但是不能通过<code>super()</code>调用父类重名方法</p>
<h5 id="set-和-get"><a href="#set-和-get" class="headerlink" title="set 和 get"></a>set 和 get</h5><p>获取时，触发 get 方法</p>
<p>修改赋值时，触发 set 方法</p>
<p>注：set 方法必须有一个参数</p>
<figure class="highlight js"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs js"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Phone</span> </span>&#123;<br>    get price () &#123;<br>        <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&#x27;价格属性被读取了&#x27;</span>);<br>        <span class="hljs-keyword">return</span> <span class="hljs-number">111</span><br>    &#125;<br><br>    set price (newV) &#123;<br>        <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&#x27;价格属性被修改了&#x27;</span>);<br>    &#125;<br>&#125;<br><br><span class="hljs-comment">// 实例化对象</span><br><span class="hljs-keyword">let</span> s = <span class="hljs-keyword">new</span> Phone();<br><span class="hljs-built_in">console</span>.log(s.price); <span class="hljs-comment">// 111	// 触发 get</span><br>s.price = <span class="hljs-string">&#x27;free&#x27;</span>;				<span class="hljs-comment">// 触发 set</span><br></code></pre></div></td></tr></table></figure>

<h3 id="2-16-数值扩展"><a href="#2-16-数值扩展" class="headerlink" title="2.16 数值扩展"></a>2.16 数值扩展</h3><h5 id="Number-EPSILON"><a href="#Number-EPSILON" class="headerlink" title="Number.EPSILON"></a>Number.EPSILON</h5><p>Number.EPSILON 是 JavaScript 表示的最小精度   <code>ε</code> </p>
<p>EPSILON属性的值接近于：2.2204460492503130808472633361816E-16</p>
<figure class="highlight js"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs js"><span class="hljs-built_in">console</span>.log(<span class="hljs-number">0.1</span> + <span class="hljs-number">0.2</span> == <span class="hljs-number">0.3</span>); 		<span class="hljs-comment">// false</span><br><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">equal</span>(<span class="hljs-params">a, b</span>) </span>&#123;<br>    <span class="hljs-keyword">if</span> (<span class="hljs-built_in">Math</span>.abs(a - b) &lt; <span class="hljs-built_in">Number</span>.EPSILON) &#123;<br>        <span class="hljs-keyword">return</span> <span class="hljs-literal">true</span><br>    &#125; <span class="hljs-keyword">else</span> &#123;<br>        <span class="hljs-keyword">return</span> <span class="hljs-literal">false</span><br>    &#125;<br>&#125;<br><span class="hljs-built_in">console</span>.log(equal(<span class="hljs-number">0.1</span> + <span class="hljs-number">0.2</span>, <span class="hljs-number">0.3</span>));	<span class="hljs-comment">// true</span><br></code></pre></div></td></tr></table></figure>

<h5 id="二进制和八进制"><a href="#二进制和八进制" class="headerlink" title="二进制和八进制"></a>二进制和八进制</h5><p>ES6 提供了二进制和八进制数值的新的写法，分别用前缀 0b 和 0o 表示。</p>
<figure class="highlight js"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs js"><span class="hljs-keyword">let</span> b = <span class="hljs-number">0b1010</span>;		<span class="hljs-comment">// 二进制</span><br><span class="hljs-keyword">let</span> b = <span class="hljs-number">0o777</span>;		<span class="hljs-comment">// 八进制</span><br><br><span class="hljs-keyword">let</span> d = <span class="hljs-number">100</span>;		<span class="hljs-comment">// 十进制</span><br><span class="hljs-keyword">let</span> x = <span class="hljs-number">0xff</span>;		<span class="hljs-comment">// 十六进制</span><br></code></pre></div></td></tr></table></figure>

<h5 id="Number-isFinite"><a href="#Number-isFinite" class="headerlink" title="Number.isFinite()"></a>Number.isFinite()</h5><p>用来检查一个数值是否为有限的，返回 boolean</p>
<h5 id="Number-isNaN"><a href="#Number-isNaN" class="headerlink" title="Number.isNaN()"></a>Number.isNaN()</h5><p>用来检查一个值是否为 NaN，返回 boolean</p>
<h5 id="Number-parseInt-amp-Number-parseFloat"><a href="#Number-parseInt-amp-Number-parseFloat" class="headerlink" title="Number.parseInt() &amp; Number.parseFloat()"></a>Number.parseInt() &amp; Number.parseFloat()</h5><p>移植到了 Number 对象下，使用不变，（截取整数，截取浮点数）</p>
<h5 id="Number-isInteger"><a href="#Number-isInteger" class="headerlink" title="Number.isInteger()"></a>Number.isInteger()</h5><p>用来判断一个数值是否为整数， 返回 boolean</p>
<h5 id="Math-trunc"><a href="#Math-trunc" class="headerlink" title="Math.trunc()"></a>Math.trunc()</h5><p>将数字的小数部分抹掉</p>
<h5 id="Math-sign"><a href="#Math-sign" class="headerlink" title="Math.sign()"></a>Math.sign()</h5><p>判断一个数是 正数(1) 负数(-1) 还是 0(0)   括号内为相应的返回值</p>
<h3 id="2-17-对象方法"><a href="#2-17-对象方法" class="headerlink" title="2.17 对象方法"></a>2.17 对象方法</h3><h5 id="Object-is-a-b"><a href="#Object-is-a-b" class="headerlink" title="Object.is(a,b)"></a>Object.is(a,b)</h5><p>判断两个值是否完全相等</p>
<p>与全等号 <code>===</code> 区别：</p>
<figure class="highlight js"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs js"><span class="hljs-built_in">console</span>.log(<span class="hljs-built_in">Object</span>.is(<span class="hljs-literal">NaN</span>, <span class="hljs-literal">NaN</span>)); <span class="hljs-comment">// true</span><br><span class="hljs-built_in">console</span>.log(<span class="hljs-literal">NaN</span> === <span class="hljs-literal">NaN</span>); 		  <span class="hljs-comment">// false</span><br></code></pre></div></td></tr></table></figure>

<h5 id="Object-assign-a-b"><a href="#Object-assign-a-b" class="headerlink" title="Object.assign(a,b)"></a>Object.assign(a,b)</h5><p>对象的合并，后面的将前面的覆盖掉</p>
<p>可以用于更新对象</p>
<figure class="highlight js"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs js"><span class="hljs-keyword">const</span> config1 = &#123;<br>    <span class="hljs-attr">host</span>: <span class="hljs-string">&#x27;localhost&#x27;</span>,<br>    <span class="hljs-attr">prot</span>: <span class="hljs-number">3306</span>,<br>    <span class="hljs-attr">name</span>: <span class="hljs-string">&#x27;root&#x27;</span>,<br>    <span class="hljs-attr">pass</span>: <span class="hljs-string">&#x27;root&#x27;</span>,<br>    <span class="hljs-attr">test</span>: <span class="hljs-string">&#x27;test&#x27;</span><br>&#125;<br><span class="hljs-keyword">const</span> config2 = &#123;<br>    <span class="hljs-attr">host</span>: <span class="hljs-string">&#x27;http://www.baidu.com&#x27;</span>,<br>    <span class="hljs-attr">prot</span>: <span class="hljs-number">33060</span>,<br>    <span class="hljs-attr">name</span>: <span class="hljs-string">&#x27;AgoniLay&#x27;</span>,<br>    <span class="hljs-attr">pass</span>: <span class="hljs-string">&#x27;020316&#x27;</span><br>&#125;<br><br><span class="hljs-built_in">console</span>.log(<span class="hljs-built_in">Object</span>.assign(config1, config2));<br><span class="hljs-comment">/*</span><br><span class="hljs-comment">	host: &quot;http://www.baidu.com&quot;</span><br><span class="hljs-comment">    name: &quot;AgoniLay&quot;</span><br><span class="hljs-comment">    pass: &quot;020316&quot;</span><br><span class="hljs-comment">    prot: 33060</span><br><span class="hljs-comment">    test: &quot;test&quot;</span><br><span class="hljs-comment">*/</span><br></code></pre></div></td></tr></table></figure>

<h5 id="Object-setPrototypeof-amp-Object-getPrototypeof"><a href="#Object-setPrototypeof-amp-Object-getPrototypeof" class="headerlink" title="Object.setPrototypeof &amp; Object.getPrototypeof"></a>Object.setPrototypeof &amp; Object.getPrototypeof</h5><p>设置原型对象，一般不使用</p>
<figure class="highlight js"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs js"><span class="hljs-built_in">console</span>.log(<span class="hljs-built_in">Object</span>.assign(config1, config2));<br><span class="hljs-comment">// Object.setPrototypeof  Object.getPrototypeof</span><br><span class="hljs-keyword">const</span> school = &#123;<br>    <span class="hljs-attr">name</span>: <span class="hljs-string">&#x27;张艺兴&#x27;</span><br>&#125;<br><span class="hljs-keyword">const</span> cities = &#123;<br>    <span class="hljs-attr">xiaoqu</span>: [<span class="hljs-string">&#x27;北京&#x27;</span>, <span class="hljs-string">&#x27;上海&#x27;</span>, <span class="hljs-string">&#x27;深圳&#x27;</span>]<br>&#125;<br><br><span class="hljs-built_in">Object</span>.setPrototypeOf(school, cities)<br><span class="hljs-built_in">console</span>.log(<span class="hljs-built_in">Object</span>.getPrototypeOf(school));<br><span class="hljs-built_in">console</span>.log(school);<br></code></pre></div></td></tr></table></figure>

<h3 id="2-18-模块化"><a href="#2-18-模块化" class="headerlink" title="2.18 模块化"></a>2.18 模块化</h3><h4 id="概述"><a href="#概述" class="headerlink" title="概述"></a>概述</h4><p>模块化是指将一个大的程序文件，拆分成许多小的文件，然后将小文件组合起来。</p>
<h5 id="好处"><a href="#好处" class="headerlink" title="好处"></a>好处</h5><ul>
<li>防止命名冲突</li>
<li>代码复用</li>
<li>高维护性</li>
</ul>
<h5 id="语法"><a href="#语法" class="headerlink" title="语法"></a>语法</h5><p>模块功能主要由两个命令构成：export 和 import</p>
<ul>
<li>export 命令用于规定模块的对外接口</li>
<li>import 命令用于输入其他模块提供的功能</li>
</ul>
<h4 id="模块化规范"><a href="#模块化规范" class="headerlink" title="模块化规范"></a>模块化规范</h4><h5 id="浏览器端"><a href="#浏览器端" class="headerlink" title="浏览器端"></a>浏览器端</h5><ol>
<li>AMD：Request.js (<a target="_blank" rel="noopener" href="http://www.requirejs.cn/">http://www.requirejs.cn/</a>)</li>
<li>CMD：Sea.js (<a target="_blank" rel="noopener" href="https://seajs.github.io/seajs/docs/">https://seajs.github.io/seajs/docs/</a>)</li>
</ol>
<h5 id="服务器端"><a href="#服务器端" class="headerlink" title="服务器端"></a>服务器端</h5><ul>
<li>CommonJS：NodeJS<ol>
<li>模块分为 单文件模块 与 包</li>
<li>模块成员导出：module.exports 和 exports</li>
<li>模块成员导入：require(‘模块标识符’)</li>
</ol>
</li>
</ul>
<h3 id="2-19-大一统的模块化规范-ES6模块化"><a href="#2-19-大一统的模块化规范-ES6模块化" class="headerlink" title="2.19 大一统的模块化规范 - ES6模块化"></a>2.19 大一统的模块化规范 - ES6模块化</h3><p>是 浏览器端 与 服务器端 通用的模块化开发规范</p>
<ul>
<li>每一个 js 文件都是一个独立的模块</li>
<li>导入模块成员使用 import 关键字</li>
<li>暴露模块成员使用 export 关键字</li>
</ul>
<h4 id="Node-js-中-babel-体验-ES6-模块化"><a href="#Node-js-中-babel-体验-ES6-模块化" class="headerlink" title="Node.js 中 babel 体验 ES6 模块化"></a>Node.js 中 babel 体验 ES6 模块化</h4><h5 id="步骤"><a href="#步骤" class="headerlink" title="步骤"></a>步骤</h5><ol>
<li><p>安装工具</p>
<figure class="highlight php"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs php">npm install --save-dev @babel/core @babel/cli @babel/preset-env @babel/node<br></code></pre></div></td></tr></table></figure>

<figure class="highlight php"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs php">npm install --save @babel/polyfill<br></code></pre></div></td></tr></table></figure></li>
<li><p>根目录下新建 babel.config.js 文件 写入：</p>
<figure class="highlight js"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs js"><span class="hljs-keyword">const</span> presets = [<br>    [<span class="hljs-string">&quot;@babel/env&quot;</span>, &#123;<br>        <span class="hljs-attr">targets</span>: &#123;<br>            <span class="hljs-attr">edge</span>: <span class="hljs-string">&quot;17&quot;</span>,<br>            <span class="hljs-attr">firefox</span>: <span class="hljs-string">&quot;60&quot;</span>,<br>            <span class="hljs-attr">chrome</span>: <span class="hljs-string">&quot;67&quot;</span>,<br>            <span class="hljs-attr">safari</span>: <span class="hljs-string">&quot;11.1&quot;</span><br>        &#125;<br>    &#125;]<br>]<br><br><span class="hljs-built_in">module</span>.exports = &#123; presets &#125;<br></code></pre></div></td></tr></table></figure></li>
<li><p>执行代码</p>
<figure class="highlight php"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs php">npx babel-node index.js<br></code></pre></div></td></tr></table></figure></li>
</ol>
<h4 id="基本语法"><a href="#基本语法" class="headerlink" title="基本语法"></a>基本语法</h4><h5 id="默认导出-与-默认导入"><a href="#默认导出-与-默认导入" class="headerlink" title="默认导出 与 默认导入"></a>默认导出 与 默认导入</h5><ul>
<li>默认导出：<code>export default 默认导出的成员</code> </li>
<li>默认导入：<code>import 接收名称 from &#39;模块标识符/路径&#39;</code> </li>
</ul>
<p>注意点：</p>
<ol>
<li>在每个模块中，只允许使用唯一的一次 export default，否则会报错</li>
<li>在模块中如果没有默认导出，那么将会 是一个空对象 <code>&#123;&#125;</code> </li>
</ol>
<h5 id="按需导出-与-按需导入"><a href="#按需导出-与-按需导入" class="headerlink" title="按需导出 与 按需导入"></a>按需导出 与 按需导入</h5><ul>
<li>按需导出：<code>export let s1 = 10</code> </li>
<li>按需导入：<code>import &#123; s1 &#125; from &#39;模块标识符/路径&#39;</code> </li>
</ul>
<p>注意点：</p>
<ol>
<li>在一个模块中，可以使用多次按需导出</li>
</ol>
<h5 id="直接导入并执行模块代码"><a href="#直接导入并执行模块代码" class="headerlink" title="直接导入并执行模块代码"></a>直接导入并执行模块代码</h5><p><code>import &#39;模块标识符/路径&#39;</code> </p>
<p>直接执行代码，没有导出。</p>

            </div>
            <hr>
            <div>
              <div class="post-metas mb-3">
                
                  <div class="post-meta mr-3">
                    <i class="iconfont icon-category"></i>
                    
                      <a class="hover-with-bg" href="/categories/%E5%89%8D%E7%AB%AF/">前端</a>
                    
                  </div>
                
                
                  <div class="post-meta">
                    <i class="iconfont icon-tags"></i>
                    
                      <a class="hover-with-bg" href="/tags/JavaScript/">JavaScript</a>
                    
                      <a class="hover-with-bg" href="/tags/%E5%89%8D%E7%AB%AF/">前端</a>
                    
                      <a class="hover-with-bg" href="/tags/ES6/">ES6</a>
                    
                      <a class="hover-with-bg" href="/tags/ECMAScript/">ECMAScript</a>
                    
                  </div>
                
              </div>
              
                <p class="note note-warning">
                  
                    本博客所有文章除特别声明外，均为本人原创，转载请注明出处！
                  
                </p>
              
              
                <div class="post-prevnext">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/2021/07/30/ES7%E7%AC%94%E8%AE%B0/">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">ES7笔记</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/2021/07/30/JavaScript%E7%AC%94%E8%AE%B0-%E5%9B%9B/">
                        <span class="hidden-mobile">JavaScript笔记(四)</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
          </article>
        </div>
      </div>
    </div>
    
      <div class="d-none d-lg-block col-lg-2 toc-container" id="toc-ctn">
        <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div class="toc-body" id="toc-body"></div>
</div>

      </div>
    
  </div>
</div>

<!-- Custom -->

  <div class="col-lg-7 mx-auto nopadding-x-md">
    <div class="container custom post-custom mx-auto">
      <img src="/img/agoni.gif" srcset="/img/loading2.gif" lazyload class="rounded mx-auto d-block mt-5" style="width:150px; height:150px;"> <h5 style="text-align: center; margin-top: 1rem;">谢谢您的光临噢！！！</h5> 
    </div>
  </div>


    

    
      <a id="scroll-top-button" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v"
                 for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>
    

    
  </main>

  <footer class="text-center mt-5 py-3">
  <div class="footer-content">
     <span>张宝文</span> <i class="iconfont icon-love"></i> <span>郑佳丽</span>  
	<!--《添加网站运行时间 -->
<br/>

<span id="timeDate">载入天数...</span><span id="times">载入时分秒...</span>
<script>
var now = new Date(); 

function createtime() {
    //此处修改你的建站时间或者网站上线时间
    var grt = new Date('07/28/2021 19:00:00');
    now.setTime(now.getTime() + 250);
    days = (now - grt) / 1000 / 60 / 60 / 24;

    dnum = Math.floor(days);
    hours = (now - grt) / 1000 / 60 / 60 - (24 * dnum);
    hnum = Math.floor(hours);
    if (String(hnum).length == 1) {
        hnum = "0" + hnum;
    }
    minutes = (now - grt) / 1000 / 60 - (24 * 60 * dnum) - (60 * hnum);
    mnum = Math.floor(minutes);
    if (String(mnum).length == 1) {
        mnum = "0" + mnum;
    }
    seconds = (now - grt) / 1000 - (24 * 60 * 60 * dnum) - (60 * 60 * hnum) - (60 * mnum);
    snum = Math.round(seconds);
    if (String(snum).length == 1) {
        snum = "0" + snum;
    }
    document.getElementById("timeDate").innerHTML = " 本站已各种夹缝中安全运行 " + dnum + " 天 ";
    document.getElementById("times").innerHTML = hnum + " 小时 " + mnum + " 分 " + snum + " 秒";
}
setInterval("createtime()", 250);
</script>

<!-- 添加网站运行时间》-->
  </div>
  
  <div class="statistics">
    
    

    
      
        <!-- 不蒜子统计PV -->
        <span id="busuanzi_container_site_pv" style="display: none">
            总访问量 
            <span id="busuanzi_value_site_pv"></span>
             次
          </span>
      
      
        <!-- 不蒜子统计UV -->
        <span id="busuanzi_container_site_uv" style="display: none">
            总访客数 
            <span id="busuanzi_value_site_uv"></span>
             人
          </span>
      
    
  </div>


  

  
</footer>


  <!-- SCRIPTS -->
  
  <script  src="https://cdn.jsdelivr.net/npm/nprogress@0.2.0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/nprogress@0.2.0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://cdn.jsdelivr.net/npm/jquery@3.6.0/dist/jquery.min.js" ></script>
<script  src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/js/bootstrap.min.js" ></script>
<script  src="/js/events.js" ></script>
<script  src="/js/plugins.js" ></script>

<!-- Plugins -->


  
    <script  src="/js/img-lazyload.js" ></script>
  



  



  <script  src="https://cdn.jsdelivr.net/npm/tocbot@4.12.3/dist/tocbot.min.js" ></script>



  <script  src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.5.7/dist/jquery.fancybox.min.js" ></script>



  <script  src="https://cdn.jsdelivr.net/npm/anchor-js@4.3.1/anchor.min.js" ></script>



  <script defer src="https://cdn.jsdelivr.net/npm/clipboard@2.0.8/dist/clipboard.min.js" ></script>



  <script  src="/js/local-search.js" ></script>



  <script defer src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js" ></script>




  <script  src="https://cdn.jsdelivr.net/npm/typed.js@2.0.12/lib/typed.min.js" ></script>
  <script src="https://sdk.jinrishici.com/v2/browser/jinrishici.js" charset="utf-8"></script>

  <script>
    function typing(id, title){
        var typed = new Typed('#' + id, {
            strings: [
              '  ',
              title + "&nbsp;",
            ],
            cursorChar: "❤",
            typeSpeed: 150,
            loop: false,
        });
        typed.stop();
        $(document).ready(function () {
            $(".typed-cursor").addClass("h2");
            typed.start();
        });
    }

    
        typing("subtitle", document.getElementById('subtitle').title)
    

  </script>













  
<script src="//cdn.jsdelivr.net/gh/bynotes/texiao/source/js/yinghua.js"></script>



<!-- 主题的启动项 保持在最底部 -->
<script  src="/js/boot.js" ></script>

</body>
</html>
